package com.springboot.auth.authorization.oauth2;

import com.springboot.auth.authorization.entity.User;
import com.springboot.auth.authorization.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service("userDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String uniqueId) {

        User user = userService.queryByMobile(uniqueId);
        log.info("load user by username :{}", user.toString());
        List<GrantedAuthority> grantedAuthorities=new ArrayList<>();

        return new org.springframework.security.core.userdetails.User(
                user.getUsername(),
                user.getPassword(),grantedAuthorities);
    }
}
